from docx import Document

def read_paragraph_by_index(file_path, paragraph_index):
    # 打开Word文档
    doc = Document(file_path)
    
    print(len(doc.paragraphs))
    for paragraph_index in range(len(doc.paragraphs)):
        # 读取指定索引的段落内容
        content = doc.paragraphs[paragraph_index].text
        print(f"段落 {paragraph_index} 的内容是: {content}")
    
    # 检查索引是否在有效范围内
    if paragraph_index < len(doc.paragraphs):
        # 读取指定索引的段落内容
        content = doc.paragraphs[paragraph_index].text
        print(f"段落 {paragraph_index} 的内容是: {content}")
    else:
        print(f"段落索引 {paragraph_index} 超出文档段落数量。")

# 使用函数读取指定位置的段落，例如读取第3个段落
file_path = 'word.docx'  # Word文件路径
paragraph_index = 20  # 索引从0开始
read_paragraph_by_index(file_path, paragraph_index)